﻿/*******************************************************************************
 * AAEngine
 * Copyright (c) 2010 Mike Jarosch
 *
 * This file is licensed under the terms of the MIT license, which is included
 * in the "MIT License.txt" file at the root directory of this SDK.
 ******************************************************************************/

using Microsoft.Xna.Framework;
using AAEngine.Engine;
using AAEngine.Engine.Components;
using AAEngine.Engine.Entities;

namespace AAEngineTest.Components
{
    public class PlayerControllerComponent : TickedComponent
    {
        private InputMap _inputMap;
        private float _left, _right;

        public PropertyReference LinearVelocityProperty { get; set; }

        public InputMap Input
        {
            get { return _inputMap; }
            set
            {
                _inputMap = value;

                if (_inputMap != null)
                {
                    _inputMap.MapActionToHandler("GoLeft", GoLeft);
                    _inputMap.MapActionToHandler("GoRight", GoRight);
                }
            }
        }

        public override void OnAdd()
        {
            base.OnAdd();
        }

        public override void OnRemove()
        {
            _inputMap.Dispose();

            base.OnRemove();
        }

        public override void OnReset()
        {
            base.OnReset();
        }

        public override void OnTick(float deltaTime)
        {
            float move = _right - _left;
            Vector2 velocity = (Vector2)Owner.GetProperty(LinearVelocityProperty);
            velocity.X = move * 100;

            Owner.SetProperty(LinearVelocityProperty, velocity);
        }

        private void GoLeft(float value)
        {
            _left = value;
        }

        private void GoRight(float value)
        {
            _right = value;
        }
    }
}
